const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const getDays = (strDateStart,strDateEnd) => {
  const strSeparator = "-"; //日期分隔符
  let oDate1;
  let oDate2;
  let iDays;
  oDate1= strDateStart.split(strSeparator);
  oDate2= strDateEnd.split(strSeparator);
  if(Number(oDate1[1]) < 10) {
      oDate1[1] = '0'+oDate1[1];
  }
  if(Number(oDate1[2]) < 10) {
      oDate1[2] = '0'+oDate1[2];
  }
  if(Number(oDate2[1]) < 10) {
      oDate2[1] = '0'+oDate2[1];
  }
  if(Number(oDate2[2]) < 10) {
      oDate2[2] = '0'+oDate2[2];
  }
  let strDateS = new Date(oDate1[0] + "-" + oDate1[1] + "-" + oDate1[2]);
  let strDateE = new Date(oDate2[0] + "-" + oDate2[1] + "-" + oDate2[2]);
  iDays = parseInt(Math.abs(strDateS - strDateE ) / 1000 / 60 / 60 /24)//把相差的毫秒数转换为天数 

  return iDays ;
};

const getStrDate = (date) => {
  var strSeparator = "-"; //日期分隔符
  if(!date) {
    date = new Date();
  }
  let month = date.getMonth() + 1;
  if(month < 10) {
    month = '0' + month;
  }
  let day = date.getDate();
  if(day < 10) {
    day = '0' + day;
  }

  return date.getFullYear() + strSeparator + month + strSeparator + day;
}

module.exports = {
  formatTime: formatTime,
  getDays: getDays,
  getStrDate: getStrDate
}
